﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MakeItWeb.Framework;
using Telerik.Web.UI;

namespace AllNations.Framework.UserControls
{
    public partial class MultiSectionDatabaseContent : MakeItWeb.Framework.MakeItWebUserControl
    {
        Guid _ContentGuid = Guid.Empty;

        public Guid ContentGuid
        {
            get { return _ContentGuid == Guid.Empty ? CurrentHtmlPage.HtmlPageId : _ContentGuid; }
            set { _ContentGuid = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            rptDatabaseContent.DataSource = MakeItWeb.Framework.DatabaseContent.ListContentByHtmlPageId(ContentGuid);
            rptDatabaseContent.DataBind();
        }

        protected void menuAdmin_ItemClick(object sender, RadMenuEventArgs e)
        {
            switch (e.Item.Value)
            {
                case "AddContentSection":
                    MakeItWeb.Framework.DatabaseContent.GetDatabaseContentByCd(Guid.NewGuid().ToString(), ContentGuid);
                    Response.Redirect(HtmlPage.GetRawUrl(), true);
                    break;
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            panelAdminButton.Visible = IsPageAdministrator();
            panelContent.CssClass = IsPageAdministrator() ? "MultiSectionDatabaseContentAdmin" : "MultiSectionDatabaseContent";
            base.OnPreRender(e);
        }

        protected void rptDatabaseContent_ItemCreated(object sender, EventArgs e)
        {
            MakeItWeb.Framework.DatabaseContent[] contents = (sender as Repeater).DataSource as MakeItWeb.Framework.DatabaseContent[];
            if (contents != null && contents.Length > 0)
            {
                IOrderable dbContent = (e as RepeaterItemEventArgs).Item.FindControl("DatabaseContent1") as IOrderable;
                if ((e as RepeaterItemEventArgs).Item.ItemIndex < contents.Length - 1)
                    dbContent.CanMoveDown = true;

                if ((e as RepeaterItemEventArgs).Item.ItemIndex > 0)
                    dbContent.CanMoveUp = true;
            }
        }
    }
}